package com.cuanbo.dualscreenteachfu.utils;

import android.content.Context;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.widget.Toast;

import com.cuanbo.dualscreenteachfu.base.App;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
 * apk安装工具类
 */
public class ApkUtil {

    /**
     * 安装apk
     *
     * @param context  上下文对象
     * @param filePath 安装包路径
     */
    public static void installApk(Context context, String filePath) {
        try {
            //MediaVolumeUtil.setVolume(context,0);
            /*AutoUpdateService.recordTime = new Date().getTime();
            File apk = new File(filePath);
            LogUtil.add("APK保存地址",""+filePath,"ApkUtil");
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                Uri uri = FileProvider.getUriForFile(context, "android.uid.system.fileprovider", apk);
                intent.setDataAndType(uri, "application/vnd.android.package-archive");
            } else {
                intent.setDataAndType(Uri.fromFile(apk), "application/vnd.android.package-archive");
            }
            try {
                LogUtil.add("","尝试拉起apk安装界面","ApkUtil");
                context.startActivity(intent);
                LogUtil.add("","拉起apk安装界面成功","ApkUtil");
            } catch (Exception e) {
                LogUtil.addError("----------------------APK安装时异常-----------------------");
                LogUtil.addError(e);
                LogUtil.addError("----------------------end-----------------------");
            }*/
            slienceInstallWithSysSign(filePath);
        } catch (Exception e) {
            LogUtil.addError("----------------------APK安装时异常-----------------------");
            LogUtil.addError(e);
            LogUtil.addError("----------------------end-----------------------");
            Toast.makeText(context, "安装时异常！", Toast.LENGTH_SHORT).show();
        }
    }

    public static boolean slienceInstallWithSysSign(String filePath) {

        File file =new File(filePath);

        PackageManager packageManager = App.getInstance().getPackageManager();

        Class pmClz =packageManager.getClass();

        try {

            if (Build.VERSION.SDK_INT >=21) {

                Class aClass =Class.forName("android.app.PackageInstallObserver");

                Constructor constructor =aClass.getDeclaredConstructor();

                constructor.setAccessible(true);

                Object installObserver =constructor.newInstance();

                Method method =pmClz.getDeclaredMethod("installPackage",Uri.class,aClass,int.class,String.class);

                method.setAccessible(true);

                method.invoke(packageManager,Uri.fromFile(file),installObserver,2,null);

            }else {

                Method method =pmClz.getDeclaredMethod("installPackage",

                        Uri.class,Class.forName("android.content.pm.IPackageInstallObserver"),int.class,String.class);

                method.setAccessible(true);

                method.invoke(packageManager,Uri.fromFile(file),null,2,null);

            }

            return true;

        }catch (Exception e) {

            LogUtil.addError("----------------------APK安装时异常-----------------------");
            LogUtil.addError(e);
            LogUtil.addError("----------------------end-----------------------");

        }

        return false;

    }
}
